package com.akkainaction.tool.extension.typedactor;

import akka.actor.ActorSystem;
import akka.actor.TypedActor;
import akka.actor.TypedActorExtension;
import akka.actor.TypedProps;
import akka.dispatch.OnSuccess;
import akka.japi.Option;
import scala.concurrent.Future;

public class TestUserService {

    public static void main(String[] args) {
        ActorSystem system = ActorSystem.create("sys");
        TypedActorExtension typedActorExtension = TypedActor.get(system);
//        在示例中，我们直接向TypedProps中传入了实现类的类型，此时它会调用默认的构造方法来创建对象，假如希望调用含有参数的其他构造方法，则需要传入Creator接口实现，然后自己构造实现类对象：
//        new TypedProps<UserServiceImpl>(UserService.class, new
//                　　 Creator<UserServiceImpl>() {
//　　　　 @Override
//　　　　 public UserServiceImpl create() throws Exception {
//　　　　　　 return new UserServiceImpl("Akka");
//　　　　 }
//        });
        UserService userService = typedActorExtension.typedActorOf(new TypedProps<>(UserService.class, UserServiceImpl.class));
        System.out.println("userService:[" + userService + "]");
        userService.saveUser("1", "afei");
        String user = userService.findUser("1");
        System.out.println(user);
        //异步执行
        Future<String> fu = userService.findUserForFuture("1");
        fu.onSuccess(new OnSuccess<String>() {
            @Override
            public void onSuccess(String s) throws Throwable {
                System.out.println(s);
            }
        }, system.dispatcher());
        //阻塞直到有返回值
        Option<String> userForOpt = userService.findUserForOpt("1");
        System.out.println(userForOpt.get());

    }
}
